package com.lgx.mobile.rest.user;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lgx.common.exception.DataException;
import com.lgx.right.entity.UserExpand;
import com.lgx.right.service.IUserExpandService;

/**
 * 
 * @author xuyichen
 *
 */
@Controller
@RequestMapping("/updateHeadImage")
public class UpdateHeadImageController {
	
	private static final Logger logger = Logger.getLogger(UpdateHeadImageController.class);

	@Autowired
	IUserExpandService userExpandService;

	public IUserExpandService getUserExpandService() {
		return userExpandService;
	}

	/**
	 * 头像设置
	 * @param userId id is key
	 * @param headImage hi is key
	 * @return 0：失败；1：成功；2：没有该用户的扩展信息；3：参数异常，有参数为空
	 */
	@RequestMapping(value="/{id}/{hi}",method=RequestMethod.GET)
	@ResponseBody
	public Object updateHeadImage(@PathVariable("id") String userId,@PathVariable("hi") String headImage){
		if(StringUtils.isNotBlank(userId)&&StringUtils.isNotBlank(headImage)){
			UserExpand userExpand = getUserExpandService().findUserExpandByUserId(Long.parseLong(userId));
			if(userExpand!=null){
				String status = "0";
				userExpand.setHeadImage(headImage);
				try {
					getUserExpandService().updateUserExpand(userExpand);
					status = "1";
				} catch (DataException e) {
					e.printStackTrace();
				}
				return status;
			}else{
				if(logger.isDebugEnabled()){
					logger.debug("updateHeadImage faild userId or headImage is error");
				}
//				"updateHeadImage faild please check userId is exists";
				return "2";
			}
		}else{
			if(logger.isDebugEnabled()){
				logger.debug("updateHeadImage faild userId or headImage is not filled");
			}
//		    "updateHeadImage faild please check userId or headImage is filled";
			return "3";
		}
	}
}
